#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "LED.h"
extern uint8_t sign;
void buzzer_Init( void)
{
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	TIM_InternalClockConfig(TIM3);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;		//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler = 1440 - 1;		//PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 50;
	
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 50;		//CCR
	
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);
	TIM_OC3Init(TIM3, &TIM_OCInitStructure);
	TIM_OC4Init(TIM3, &TIM_OCInitStructure);
	
	TIM_Cmd(TIM3, ENABLE);
}

void buzzer_On( void)
{	
	TIM_SetCompare2(TIM3, 0);

}

void buzzer_Off( void)
{
	TIM_SetCompare2(TIM3, 20000);
}

void buzzer_turn( int f)
{
	if( f < 0) f = 0;
	if( f > 20000) f = 20000;
	TIM_SetCompare2(TIM3, f);
}

void Sound_SetHZ(uint16_t a)
{
	TIM_PrescalerConfig(TIM3,a,TIM_PSCReloadMode_Immediate);
}
void Play_Music(int a,int b,int c)
{
	buzzer_Init();
	Sound_SetHZ(a);Delay_ms(b);Sound_SetHZ(20);Delay_ms(c);
}

int i,time,j,a[]={                                                           //??
		    1635,1376, 917,1376,2062,1836,1635, 917,1376,1376,1836, 917,1376,1376,
		     917,1456, 917,1635,1376, 917,1376,2062,1836,1635, 917,1376,1376,1836,
	         917,1376,1376, 917,1836,1376, 917,                             //?? 34
	          30, 917, 917,1376,1376,1226,1092,                              //?????? 41
	          30, 917, 917,1376,1376,1226,1092,1226,1376,1836,               //???????? 51  
	          30, 917, 917,1376,1376,1226,1092,                              //?????? 58 
	          30,1092,1226,1092,1031,1092,1226,1031,1092,1226,1376,          //????????? 69
	        1836,1376,1376,1092,1031,1092,1226,                              //Re So So Si Do Si La 76
	        1376,1226,1092,1092,1092,1092,1226,1092,1226,1376,               //So La Si Si Si Si La Si La So 86 
	        1836,1376,1226,1092,1031,1092,1226,1376,                         //???? ???? 94
	        1226,1092,1092,1092,1092,1226,1092,1226,1376,                    //????????? 103 
	          30,1376,1376,1376,1376,1635,1456,1376, 917,1031,1092,1376,1367,//??? ????????? 116
	          30,1376,1376,1376,1376,1092,1376,                               //?????? 123
	        1635,1456,1376, 917,1031,1092,1376,1226,                          //???????? 131
	        1092,1226,1031,1092,1376, 917, 728, 687, 728, 917,1376,           //???? ??????? 142
        	  30,1376, 817, 817,  30, 817, 917, 917,                          //??? ??? 150
	          30, 917,1031,1092,1226,1092,1031,1092,                          //??????? 158
	          30,1092,1031, 917,1092,  30,1031, 917, 728, 612, 728, 687, 687, //???? ??????? 171 
	          30, 687, 687, 917, 917, 817, 917,1031,                          //??????? 179
	        1226,1092, 1031, 917, 817,1376,817, 728, 728,                     //????????? 188
	        1092,1226,1031,1092,  30,1376, 917, 728, 687, 728, 917,1376,      //???? ??????? 200
     		  30,1376, 817, 817,  30, 817, 917, 917,                          //??? ??? 208
			  30, 917,1031,1092,1226,1092,1031,1092,                          //??????? 216
			  30,1092,1031, 917,1092,  30,1031, 917, 728, 612, 728, 687, 687, //???? ??????? 229
			  30,1376, 687, 917, 917, 817, 917,                               //?????? 236
			1031,1635,1456,1376,1226,1092,1226,30,1092,1376                   //??????? ?? 246
		  };

int tm[]={                                                  //????
		  50, 50, 50, 50, 50, 25, 25, 50, 50, 50, 50, 50, 50, 50,
	      50, 50, 50, 50, 50, 50, 50, 50, 25, 25, 50, 50, 50, 50,
	      50, 50, 50, 50, 25, 25, 50,                       //??  
	      50, 50, 50, 50,100, 50, 50,                       //??????
          50, 50, 50, 50, 50, 25, 25, 25, 25, 50,           //????????
       	  50, 50, 50, 50,100, 50, 50,                       //??????  
	      50,100, 25, 25, 25, 25, 25, 25, 25, 25, 50,       //?????????
     	  50, 50, 50, 50, 50, 50, 50,                        //Re So So Si Do Si La
      	  25, 25, 50, 50, 50, 50, 25, 25, 50,100,            //So La Si Si Si Si La Si La So 
	      50, 50, 50, 50, 50, 50, 50, 25,                    //???? ????
     	  25, 50, 50, 50, 50, 25, 25, 50, 75,                //????????? 
	     400, 25, 25, 25, 25, 50, 50, 50, 50, 50, 50, 50,125,//??? ????????? 
	      50, 25, 25, 25, 25, 50, 50,                        //??????
	      50, 50, 50, 50, 50, 50, 50,130,                    //????????
	      50, 50, 50,100, 50, 50, 50, 50, 50, 50, 50,        //???? ???????
	      50, 50, 50, 50, 50, 50, 50, 50,                    //??? ??? 
	      50, 50, 50, 50, 50, 50, 50,125,                    //???????
	      75, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,125,//???? ???????  
	      50, 50, 50, 50, 50, 50, 50, 50,                    //???????
	      50, 50, 50, 50, 50, 50, 75, 25,100,                //?????????
	      50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,    //???? ???????
		  50, 50, 50, 50, 50, 50, 50, 50,                    //??? ??? 
		  50, 50, 50, 50, 50, 50, 50,150,                    //???????
		  50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,125,//???? ??????? 
		  50, 50, 50, 50, 50, 50, 50,                        //??????
		  50, 50, 50, 50, 50, 50,100, 20, 50,200,            //??????? ??
		};
void show_tone(void)
{
	switch (a[i])
    {
    	case 2747:OLED_ShowString(2,6,"-C");
    		break;
    	case 2448:OLED_ShowString(2,6,"-D");
    		break;
     	case 2181:OLED_ShowString(2,6,"-E");
    		break;
    	case 2062:OLED_ShowString(2,6,"-F");
    		break;
    	case 1836:OLED_ShowString(2,6,"-G");
    		break;
    	case 1635:OLED_ShowString(2,6,"-A");
    		break;
    	case 1456:OLED_ShowString(2,6,"-B");
    		break;
		case 1376:OLED_ShowString(2,6," C");
    		break;
    	case 1226:OLED_ShowString(2,6," D");
    		break;
     	case 1092:OLED_ShowString(2,6," E");
    		break;
    	case 1031:OLED_ShowString(2,6," F");
    		break;
    	case 917:OLED_ShowString(2,6," G");
    		break;
    	case 817:OLED_ShowString(2,6," A");
    		break;
    	case 728:OLED_ShowString(2,6," B");
    		break;
		case 687:OLED_ShowString(2,6,"+C ");
    		break;
    	case 612:OLED_ShowString(2,6,"+D ");
    		break;
     	case 545:OLED_ShowString(2,6,"+E ");
    		break;
    	case 514:OLED_ShowString(2,6,"+F ");
    		break;
    	case 458:OLED_ShowString(2,6,"+G ");
    		break;
    	case 408:OLED_ShowString(2,6,"+A ");
    		break;
    	case 363:OLED_ShowString(2,6,"+B ");
    		break;
		default:
    		break;
    }
}	
void B_Music(void)
{	
	
	OLED_ShowString(2,1,"tone:");
	OLED_ShowString(1,4,"QingTian ");
	int c;
	for(i=0;i<=246;i++)
	{
        LED_1_On();
		if( sign == 2) 
		{
			sign = 0;
			break;
		}			
		
	
		show_tone();
		c=5;j=tm[i]/25;
		time=j*180;
		if(i==49||i==67||i==178)
		{
			c=0;
		}
		LED_1_On();
		Play_Music(a[i],time,c);
		LED_1_On();
	}
}
